/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InicioSesion.java
 *
 * Created on 23/06/2010, 09:44:42 PM
 */

package CapaGrafica;
import javax.swing.JOptionPane;
import CapaLogica.ComunicacionServidor;
/**
 *
 * @author Maria
 */
public class InicioSesion extends javax.swing.JFrame {
    private MenuUsuario ventanaPrincipal;
    /** Creates new form InicioSesion */
    public InicioSesion(MenuUsuario ventanaMadre) {
        initComponents();
        ventanaPrincipal = ventanaMadre;
    }
    public MenuUsuario getMain() {
        return this.ventanaPrincipal;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBiniciarSesion = new javax.swing.JButton();
        jBregistar = new javax.swing.JButton();
        jPFclave = new javax.swing.JPasswordField();
        jLclave = new javax.swing.JLabel();
        jLusuario = new javax.swing.JLabel();
        jTFusuario = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        setLocationByPlatform(true);
        setResizable(false);

        jBiniciarSesion.setText("inicio");
        jBiniciarSesion.setName("jBiniciarSesion"); // NOI18N
        jBiniciarSesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBiniciarSesionActionPerformed(evt);
            }
        });

        jBregistar.setText("registrarse");
        jBregistar.setName("jBregistar"); // NOI18N
        jBregistar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBregistarActionPerformed(evt);
            }
        });

        jPFclave.setName("jPFclave"); // NOI18N

        jLclave.setText("Clave");
        jLclave.setName("jLclave"); // NOI18N

        jLusuario.setText("Usuario");
        jLusuario.setName("jLusuario"); // NOI18N

        jTFusuario.setName("jTFusuario"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBiniciarSesion, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
                        .addComponent(jBregistar)
                        .addGap(62, 62, 62))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLclave)
                            .addComponent(jLusuario))
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTFusuario)
                            .addComponent(jPFclave, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
                        .addContainerGap(133, Short.MAX_VALUE))))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLusuario)
                    .addComponent(jTFusuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPFclave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLclave))
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBiniciarSesion)
                    .addComponent(jBregistar))
                .addGap(70, 70, 70))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBiniciarSesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBiniciarSesionActionPerformed
    String mensajeError = "El nombre de usuario y la contraseña que ingresó no coinciden";
        String mensajeLogin = ComunicacionServidor.login (this.jTFusuario.getText(), this.jPFclave.getPassword());

        String resultadoLogin[] = mensajeLogin.split(":");

        if (resultadoLogin[0].equals("TRUE"))
        {
            this.ventanaPrincipal.setEnabled(true);
            this.ventanaPrincipal.setNombreCliente(resultadoLogin[1]);
            this.ventanaPrincipal.setApellidoCliente(resultadoLogin[2]);
            this.ventanaPrincipal.setCedulaCliente(resultadoLogin[3]);
            this.dispose();
        }
        else
        {
            JOptionPane.showMessageDialog (this, mensajeError);
            this.jPFclave.setText("");
        }       // TODO add your handling code here:
    }//GEN-LAST:event_jBiniciarSesionActionPerformed

    private void jBregistarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBregistarActionPerformed
     FormularioCliente ventanaRegistro = new FormularioCliente(this);
        ventanaRegistro.setVisible(true);
        this.setVisible(false);   // TODO add your handling code here:
    }//GEN-LAST:event_jBregistarActionPerformed

    /**
    * @param args the command line arguments
    */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBiniciarSesion;
    private javax.swing.JButton jBregistar;
    private javax.swing.JLabel jLclave;
    private javax.swing.JLabel jLusuario;
    private javax.swing.JPasswordField jPFclave;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTFusuario;
    // End of variables declaration//GEN-END:variables

}
